<script>
import ElImageViewer from 'element-ui/packages/image/src/image-viewer';

export default {
  name: "Job.vue",
  components: {ElImageViewer},
  data() {
    return {
      tableData: [],
      form: {},
      currentPage: 1,
      pageSize: 5,
      total: 0,
      visible: false,
      loading: true,
      jobName: null,
      jobGroupName: null,
      quartzJobName: null,
      quartzJobGroupName: null,
      status: null,
      multipleSelection: [],
      dialogFormVisible: false,
      dialogFormVisible1: false,
      dialogFormVisible2: false,
      formLabelWidth: '70px',
      showCron: false,
      expression: '',
      statusOptions: [{
        value: true,
        label: '正常'
      }, {
        value: false,
        label: '暂停'
      }]
    }
  },
  created() {
    this.load();
  },
  methods: {
    load() {
      this.request.get("/job/getQuartzJobList", {
        params: {
          jobName: this.jobName,
          jobGroupName: this.jobGroupName,
          status: this.status,
          currentPage: this.currentPage,
          pageSize: this.pageSize,
        }
      }).then(res => {
        this.tableData = res.data.records;
        this.loading = false;
        this.total = res.data.total;
      });

    },
    handleSizeChange(val) {
      this.pageSize = val;
      this.load();

    },
    handleCurrentChange(val) {
      this.currentPage = val;
      this.load();
    },
    reset() {
      this.jobName = null;
      this.jobGroupName = null;
      this.status = null;
      this.tableData = [];
      this.currentPage = 1;
      this.pageSize = 5;
      this.total = 0;
      this.load();
    },
    handleDelete(job) {
      this.request.delete("/job/deleteJob", {
        params: {
          jobName: job.jobName,
          jobGroupName: job.jobGroupName,
        }
      }).then(res => {
        if (res.code === 200) {
          this.$message({
            message: '删除成功',
            type: 'success',
            center: true
          });
          this.load();
        } else {
          this.$message({
            showClose: true,
            message: res.msg,
            type: 'error'
          });
        }
      })
    },
    handleSelectionChange(val) {
      this.multipleSelection = val.map((a) => {
        return {'jobName': a.jobName, 'jobGroupName': a.jobGroupName}
      });
      console.log(this.multipleSelection);
    },
    handleBatchRemove() {
      let selectedJobs = this.multipleSelection;
      this.request.post("/job/deleteJobs", selectedJobs).then(res => {
        if (res.code == 200) {
          this.$message({
            message: '删除成功',
            type: 'success',
            center: true
          });
          this.load();
        } else {
          this.$message({
            showClose: true,
            message: res.msg,
            type: 'error'
          });
        }
      })
    },
    changeEnable(job) {
      this.request.get("/job/updateJobStatus" , {
        params: {
          jobName:job.jobName,
          jobGroupName:job.jobGroupName,
          status: job.status
        }
      }).then(res => {
        if (res.code === 200) {
            this.$message({
              message: job.status===true ?'任务已启动':'任务已暂停',
              type: 'success',
              center: true
            });
            this.load();
        } else {
          this.$message({
            message: res.msg,
            type: 'error',
            center: true
          });
        }
      })
    },
    viewJob(job){
      this.form=job;
      this.dialogFormVisible1=true;
    },
    handleEdit(job) {
      this.form = job;
      this.dialogFormVisible2 = true;
      this.quartzJobName = job.jobName;
      this.quartzJobGroupName = job.jobGroupName;
    },
    edit(job) {
      this.request.get("/job/updateJob", {
        params: {
          cronExpression: this.form.cronExpression,
          description: this.form.description,
          jobClassName: this.form.jobClassName,
          jobGroupName: this.form.jobGroupName,
          jobName: this.form.jobName,
          status: this.form.status,
          triggerGroupName: this.form. triggerGroupName,
          triggerName: this.form.triggerName,
          quartzJobName:this.quartzJobName,
          quartzJobGroupName:this.quartzJobGroupName
        }
      }).then(res => {
        if (res.code === 200) {
          this.dialogFormVisible2 = false;
          this.form = {};
          this.load();
          this.$message({
            message: '任务修改成功',
            type: 'success',
            center: true
          });
        } else {
          this.$message({
            message: res.msg,
            type: 'error',
            center: true
          });
        }
      });
    },
    handleSave() {
      this.form = {};
      this.dialogFormVisible = true;
    },
    save() {
      let quartzJob = this.form;
      this.request.post("/job/addJob", quartzJob).then(res => {
        if (res.code === 200) {
          this.dialogFormVisible = false;
          this.form = {};
          this.load();
          this.$message({
            message: '任务创建成功',
            type: 'success',
            center: true
          });
        } else {
          this.$message({
            message: res.msg,
            type: 'error',
            center: true
          });
        }
      });
    },
    headerBg({row, rowIndex}) {
      if (rowIndex == 0) {
        return 'headerBgClass';
      }
    },
    crontabFill(value) {
      this.form.cronExpression = value;
    },
    showCronDialog() {
      this.expression = this.form.cronExpression;
      this.showCron = true;
    },
    exp() {
      window.open(this.baseUrl + '/user/export')
      // this.request.get("/user/export",{
      //   responseType: 'blob'
      // }).then((res)=>{
      //    if(!res){
      //      return;
      //    }
      //   let blob;
      //
      //   let fileName=window.decodeURI(res.headers['content-disposition'].split('=')[1]);
      //   let url = window.URL.createObjectURL(blob);
      //   let aLink = document.createElement("a");
      //   aLink.style.display = "none";
      //   aLink.href = url;
      //   aLink.setAttribute("download", fileName);
      //   document.body.appendChild(aLink);
      //   aLink.click();
      //   document.body.removeChild(aLink);
      //   window.URL.revokeObjectURL(url);
      //   return this.$message.success("导出成功");
      // })
    }
  }

}
</script>

<template>
  <div>
    <div>
      <div>
        <el-input suffix-icon="el-icon-search" placeholder="任务名称" v-model="jobName"
                  style="width:160px"></el-input>
        <el-input suffix-icon="el-icon-search" placeholder="任务组名" v-model="jobGroupName"
                  style="margin-left: 5px;width:160px"></el-input>
        <el-select v-model="status" clearable placeholder="任务状态" style=" margin-left: 5px;width:160px">
          <el-option v-for="item in statusOptions" :key="item.value" :label="item.label"
                     :value="item.value"></el-option>
        </el-select>
        <el-button type="primary" style="margin-left: 5px" @click="load">搜索</el-button>
        <el-button type="primary" style="margin-left: 5px" @click="reset">重置</el-button>
      </div>
    </div>
    <div style="margin:10px 0px">
      <el-button type="success" icon="el-icon-circle-plus-outline" @click="handleSave">新增</el-button>
      <el-dialog title="创建任务" :visible.sync="dialogFormVisible" width="30%" center>
        <el-form :model="form" label-width="100px" style="width:340px">
          <el-form-item label="任务名称">
            <el-input v-model="form.jobName" placeholder="请输入任务名称"></el-input>
          </el-form-item>
          <el-form-item label="任务组名">
            <el-input v-model="form.jobGroupName" placeholder="请输入任务组名"></el-input>
          </el-form-item>
          <el-form-item label="任务类名">
            <el-input v-model="form.jobClassName" placeholder="请输入任务类名"></el-input>
          </el-form-item>
          <el-form-item label="触发器名称">
            <el-input v-model="form.triggerName" placeholder="请输入触发器名称"></el-input>
          </el-form-item>
          <el-form-item label="触发器组名">
            <el-input v-model="form.triggerGroupName" placeholder="请输入触发器组名"></el-input>
          </el-form-item>
          <el-form-item label="cron表达式">
            <el-input v-model="form.cronExpression" placeholder="请输入cron表达式" style="width:140px"></el-input>&ensp;<el-button
              type="info" @click="showCronDialog">生成表达式
          </el-button>
            <el-dialog title="生成 cron" append-to-body=true :visible.sync="showCron">
              <vcrontab @hide="showCron=false" @fill="crontabFill" :expression="expression"></vcrontab>
            </el-dialog>
          </el-form-item>
          <el-form-item label="状态">
            <el-input v-model="form.status" placeholder="请输入任务名称"></el-input>
          </el-form-item>
          <el-form-item label="任务描述">
            <el-input v-model="form.description" placeholder="请输入任务描述"></el-input>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="primary" @click="save">确 定</el-button>
          <el-button @click="dialogFormVisible = false">取 消</el-button>
        </div>
      </el-dialog>
      <el-popconfirm
          confirm-button-text='确定'
          cancel-button-text='我再想想'
          icon="el-icon-info"
          icon-color="red"
          title="你确定批量删除吗？"
          @confirm="handleBatchRemove"
          class="ml-5"
      >
        <el-button type="danger" icon="el-icon-remove-outline" slot="reference">批量删除</el-button>
      </el-popconfirm>
      <el-button type="primary" icon="el-icon-top" style="margin-left: 5px" @click="exp">导出</el-button>
    </div>
    <el-table :data="tableData"
              border stripe
              :header-cell-class-name="headerBg"
              max-height="550"
              v-loading="loading"
              ref="filterTable"
              element-loading-text="拼命加载中"
              element-loading-spinner="el-icon-loading"
              element-loading-background="rgba(255,225,255, 0.1)"
              @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55px" align="center"></el-table-column>
      <el-table-column prop="jobName" label="任务名称" width="130px" align="center">
      </el-table-column>
      <el-table-column prop="jobGroupName" label="任务组名称" width="120px" :show-overflow-tooltip=true align="center">
      </el-table-column>
      <el-table-column prop="jobClassName" label="任务类名" width="200px" :show-overflow-tooltip=true align="center">
      </el-table-column>
      <el-table-column prop="cronExpression" label="cron表达式" width="200px" :show-overflow-tooltip=true
                       align="center">
      </el-table-column>
      <el-table-column prop="timeZoneId" label="时区" width="220px" :show-overflow-tooltip=true align="center">
      </el-table-column>
      <el-table-column label="状态" width="70px">
        <template slot-scope="scope">
          <el-switch
              v-model="scope.row.status"
              active-color="#13ce66"
              inactive-color="#ccc"
              @change="changeEnable(scope.row)"
          >
          </el-switch>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="300px" align="center">
        <template slot-scope="scope">
          <el-popconfirm
              confirm-button-text='确定'
              cancel-button-text='我再想想'
              icon="el-icon-info"
              icon-color="red"
              title="你确定删除吗？"
              @confirm="handleDelete(scope.row)"
              class="ml-5"
          >
            <el-button type="danger" icon="el-icon-delete" slot="reference">删除</el-button>
          </el-popconfirm>
          <el-button type="warning" icon="el-icon-view el-icon--right" style="margin-left:5px"
                     @click="handleEdit(scope.row)">修改
          </el-button>
          <el-dialog title="修改任务" :visible.sync="dialogFormVisible2" width="30%" center>
            <el-form :model="form" label-width="100px" style="width:340px">
              <el-form-item label="任务名称">
                <el-input v-model="form.jobName" placeholder="请输入任务名称"></el-input>
              </el-form-item>
              <el-form-item label="任务组名">
                <el-input v-model="form.jobGroupName" placeholder="请输入任务组名"></el-input>
              </el-form-item>
              <el-form-item label="任务类名">
                <el-input v-model="form.jobClassName" placeholder="请输入任务类名"></el-input>
              </el-form-item>
              <el-form-item label="触发器名称">
                <el-input v-model="form.triggerName" placeholder="请输入触发器名称"></el-input>
              </el-form-item>
              <el-form-item label="触发器组名">
                <el-input v-model="form.triggerGroupName" placeholder="请输入触发器组名"></el-input>
              </el-form-item>
              <el-form-item label="cron表达式">
                <el-input v-model="form.cronExpression" placeholder="请输入cron表达式" style="width:140px"></el-input>&ensp;<el-button
                  type="info" @click="showCronDialog">生成表达式
              </el-button>
                <el-dialog title="生成 cron" append-to-body=true :visible.sync="showCron">
                  <vcrontab @hide="showCron=false" @fill="crontabFill" :expression="expression"></vcrontab>
                </el-dialog>
              </el-form-item>
              <el-form-item label="状态">
                <el-input v-model="form.status" placeholder="请输入任务名称"></el-input>
              </el-form-item>
              <el-form-item label="任务描述">
                <el-input v-model="form.description" placeholder="请输入任务描述"></el-input>
              </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
              <el-button type="primary" @click="edit(scope.row)">确 定</el-button>
              <el-button @click="dialogFormVisible2 = false">取 消</el-button>
            </div>
          </el-dialog>
          <el-button type="info" icon="el-icon-view el-icon--right" style="margin-left:5px"
                     @click="viewJob(scope.row)">查看
          </el-button>
          <el-dialog title="定时任务详情" :visible.sync="dialogFormVisible1" width="70%" center="false">
            <el-descriptions size="medium">
              <el-descriptions-item><span slot="label" style="font-weight: bold">任务名称</span>{{form.jobName }}
              </el-descriptions-item>
              <el-descriptions-item><span slot="label"
                                          style="font-weight: bold">任务组名</span>{{ form.jobGroupName }}
              </el-descriptions-item>
              <el-descriptions-item><span slot="label"
                                          style="font-weight: bold">任务类名</span>{{ form.jobClassName }}
              </el-descriptions-item>
              <el-descriptions-item><span slot="label"
                                          style="font-weight: bold">触发器名称</span>{{ form.triggerName }}
              </el-descriptions-item>
              <el-descriptions-item><span slot="label"
                                          style="font-weight: bold">触发器组名</span>{{ form.triggerGroupName }}
              </el-descriptions-item>
              <el-descriptions-item><span slot="label"
                                          style="font-weight: bold">cron表达式</span>{{ form.cronExpression }}
              </el-descriptions-item>
              <el-descriptions-item><span slot="label" style="font-weight: bold">时区</span>{{ form.timeZoneId }}
              </el-descriptions-item>
              <el-descriptions-item><span slot="label" style="font-weight: bold">状态</span>{{ form.status }}
              </el-descriptions-item>
              <el-descriptions-item><span slot="label" style="font-weight: bold">描述</span>{{ form.description }}
              </el-descriptions-item>
              <el-descriptions-item><span slot="label"
                                          style="font-weight: bold">上一次执行时间</span>{{ form.prevTime }}
              </el-descriptions-item>
              <el-descriptions-item><span slot="label"
                                          style="font-weight: bold">下一次执行时间</span>{{ form.nextTime }}
              </el-descriptions-item>
            </el-descriptions>
            <div slot="footer" class="dialog-footer">
              <el-button @click="dialogFormVisible1 = false">关闭</el-button>
            </div>
          </el-dialog>
        </template>
      </el-table-column>
    </el-table>
    <div style="padding:10px 0px">
      <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="currentPage"
          :page-sizes="[5, 10, 15, 20]"
          :page-size="pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total">
      </el-pagination>
    </div>
  </div>
</template>

<style scoped>
.headerBgClass {
  background-color: #cccccc !important;
}
</style>

